{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cvxpy as cvx\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "alpha = np.array([0.1, 0.5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimal value of x: 0.000000, 1.000000\n",
      "Optimal value of the objective: -0.500000\n"
     ]
    }
   ],
   "source": [
    "x = cvx.Variable(2)\n",
    "objective = cvx.Minimize(-alpha.T*x)\n",
    "constraints = [cvx.norm(x, 1)<=1] # leverage constraint\n",
    "problem = cvx.Problem(objective, constraints)\n",
    "result = problem.solve()\n",
    "print('Optimal value of x: {:.6f}, {:.6f}'.format(x.value[0], x.value[1]))\n",
    "print('Optimal value of the objective: {:.6f}'.format(problem.value))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Under the constraint that the sum of the absolute values of the entries in x be 1, the optimizer but all the possible value of x in the second entry, since that asset had the maximum alpha value. \n",
    "\n",
    "Let's say we don't want all our weight on one asset. Let's add a regularization term."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll use the parameter `l` as our \"dial\", with which to tune the relative importance of maximizing alphas and spreading the weight amongst all entries of x."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 236,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "l = 0.509902"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 237,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimal value of x: 0.000335, 0.001674\n",
      "Optimal value of the objective: 0.000000\n"
     ]
    }
   ],
   "source": [
    "x = cvx.Variable(2)\n",
    "objective = cvx.Minimize(-alpha.T*x + l*cvx.norm(x, 2))\n",
    "constraints = [cvx.norm(x, 1)<=1] # leverage constraint\n",
    "problem = cvx.Problem(objective, constraints)\n",
    "result = problem.solve()\n",
    "print('Optimal value of x: {:.6f}, {:.6f}'.format(x.value[0], x.value[1]))\n",
    "print('Optimal value of the objective: {:.6f}'.format(problem.value))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
